package com.bjpowernode.threadpool;

import java.util.concurrent.*;

/**
 * ThreadPoolExecutor线程池
 *
 * @author Cat老师，关注我，抖音搜索：java512
 */
public class Test03 {

    public static void main(String[] args) {

        //基于Executor框架实现线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                12,
                5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                //Executors.defaultThreadFactory(),
                //Executors.privilegedThreadFactory(),
                new MyThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()
        );

        threadPoolExecutor.execute(() -> {
            System.out.println(Thread.currentThread().getName());
        });

        threadPoolExecutor.shutdown();
    }

    /**
     * 自己实现线程工厂
     */
    static class MyThreadFactory implements ThreadFactory {

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "my-thread");
        }
    }
}